package com.inei.cie.capitulov;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.inei.cie.R;
import com.inei.cie.capituloix.CapituloIX_Activity;
import com.inei.cie.dao.CapituloVDao;
import com.inei.cie.entidades.P5;

public class CapituloV_Edificaciones_Activity extends Activity {

	// Componentes
	public EditText etTotalE1, etTotalE2, etTotalE3, etTotalE4, etTotalE5,
			etTotalE6, etTotalE7, etObs;

	public TextView tvTitulo;

	// Data
	private CapituloVDao cap5Dao;
	public String id_local;
	public int P5_NroPred;
	public int nroPredios;
	public P5 obj;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capitulo_v_edificaciones);

		IniciarComponentes();
	}

	public void IniciarComponentes() {
		// Data
		cap5Dao = new CapituloVDao(getApplicationContext());
		id_local = getIntent().getExtras().getString("id_local");
		P5_NroPred = getIntent().getExtras().getInt("Nro_Pred");
		nroPredios = getIntent().getExtras().getInt("nroPredios");

		tvTitulo = (TextView) findViewById(R.id.tvTitulo);
		tvTitulo.setText("Local Nro. " + id_local);

		// Componentes
		etTotalE1 = (EditText) findViewById(R.id.etTotalE1);
		etTotalE2 = (EditText) findViewById(R.id.etTotalE2);
		etTotalE3 = (EditText) findViewById(R.id.etTotalE3);
		etTotalE4 = (EditText) findViewById(R.id.etTotalE4);
		etTotalE5 = (EditText) findViewById(R.id.etTotalE5);
		etTotalE6 = (EditText) findViewById(R.id.etTotalE6);
		etTotalE7 = (EditText) findViewById(R.id.etTotalE7);
		etObs = (EditText) findViewById(R.id.etObs);

		obj = new P5();

		cargarData();
		if (obj == null) {
			Toast.makeText(this, "Nulo " + P5_NroPred, Toast.LENGTH_LONG)
					.show();
		}
	}

	public void cargarData() {
		cap5Dao.crearP5Edificaciones(id_local, P5_NroPred);
		obj = cap5Dao.obtenerP5Local(id_local, P5_NroPred);
		if (obj != null) {
			if (obj.getP5_Obs() != null) {
				etObs.setText(obj.getP5_Obs());
			}

			if (obj.getP5_Tot_E() == 0 && !obj.isSwCompleto()) {
				etTotalE1.setText("");
			} else {
				if (obj.getP5_Tot_E() < 10) {
					etTotalE1.setText(obj.getP5_Tot_E() != 0 ? "0"
							+ obj.getP5_Tot_E() + "" : "00");
				} else {
					etTotalE1.setText(obj.getP5_Tot_E() != 0 ? obj
							.getP5_Tot_E() + "" : "00");
				}
			}

			if (obj.getP5_Tot_P() == 0 && !obj.isSwCompleto()) {
				etTotalE2.setText("");
			} else {
				if (obj.getP5_Tot_P() < 10) {
					etTotalE2.setText(obj.getP5_Tot_P() != 0 ? "0"
							+ obj.getP5_Tot_P() + "" : "00");
				} else {
					etTotalE2.setText(obj.getP5_Tot_P() != 0 ? obj
							.getP5_Tot_P() + "" : "00");
				}
			}

			if (obj.getP5_Tot_LD() == 0 && !obj.isSwCompleto()) {
				etTotalE3.setText("");
			} else {
				if (obj.getP5_Tot_LD() < 10) {
					etTotalE3.setText(obj.getP5_Tot_LD() != 0 ? "0"
							+ obj.getP5_Tot_LD() + "" : "00");
				} else {
					etTotalE3.setText(obj.getP5_Tot_LD() != 0 ? obj
							.getP5_Tot_LD() + "" : "00");
				}
			}

			if (obj.getP5_Tot_CTE() == 0 && !obj.isSwCompleto()) {
				etTotalE4.setText("");
			} else {
				if (obj.getP5_Tot_CTE() < 10) {
					etTotalE4.setText(obj.getP5_Tot_CTE() != 0 ? "0"
							+ obj.getP5_Tot_CTE() + "" : "00");
				} else {
					etTotalE4.setText(obj.getP5_Tot_CTE() != 0 ? obj
							.getP5_Tot_CTE() + "" : "00");
				}
			}

			if (obj.getP5_Tot_MC() == 0 && !obj.isSwCompleto()) {
				etTotalE5.setText("");
			} else {
				if (obj.getP5_Tot_MC() < 10) {
					etTotalE5.setText(obj.getP5_Tot_MC() != 0 ? "0"
							+ obj.getP5_Tot_MC() + "" : "00");
				} else {
					etTotalE5.setText(obj.getP5_Tot_MC() != 0 ? obj
							.getP5_Tot_MC() + "" : "00");
				}
			}

			if (obj.getP5_Tot_L1() == 0 && !obj.isSwCompleto()) {
				etTotalE6.setText("");
			} else {
				if (obj.getP5_Tot_L1() < 10) {
					etTotalE6.setText(obj.getP5_Tot_L1() != 0 ? "0"
							+ obj.getP5_Tot_L1() + "" : "00");
				} else {
					etTotalE6.setText(obj.getP5_Tot_L1() != 0 ? obj
							.getP5_Tot_L1() + "" : "00");
				}
			}

			if (obj.getP5_Tot_R() == 0 && !obj.isSwCompleto()) {
				etTotalE7.setText("");
			} else {
				if (obj.getP5_Tot_R() < 10) {
					etTotalE7.setText(obj.getP5_Tot_R() != 0 ? "0"
							+ obj.getP5_Tot_R() + "" : "00");
				} else {
					etTotalE7.setText(obj.getP5_Tot_R() != 0 ? obj
							.getP5_Tot_R() + "" : "00");
				}
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu_capitulov_edificaciones, menu);
		menu.getItem(0).setTitle("CAPITULOS");
		menu.getItem(1).setTitle("GUARDAR");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir ?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							System.exit(0);
						}
					});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		case R.id.mnIncio:
			Intent menu_capitulo_5 = new Intent(this, CapituloV_Menu.class);
			menu_capitulo_5.putExtras(getIntent().getExtras());
			startActivity(menu_capitulo_5);
			finish();
			break;
		case R.id.mnGuardar:
			// guardar();
			if (etTotalE1.getText().toString().equals("")
					|| etTotalE2.getText().toString().equals("")
					|| etTotalE3.getText().toString().equals("")
					|| etTotalE4.getText().toString().equals("")
					|| etTotalE5.getText().toString().equals("")
					|| etTotalE6.getText().toString().equals("")
					|| etTotalE7.getText().toString().equals("")) {
				Toast.makeText(CapituloV_Edificaciones_Activity.this,
						"Debe ingresar todos los datos", Toast.LENGTH_SHORT)
						.show();
			} else {
				int tot_e = 0;
				int tot_p = 0;
				int tot_ld = 0;
				int tot_cte = 0;
				int tot_mc = 0;
				int tot_l1 = 0;
				int tot_r = 0;
				obj = cap5Dao.obtenerP5Local(id_local, P5_NroPred);
				if (etTotalE1.getText().toString().isEmpty()) {
					tot_e = 0;
				} else {
					tot_e = Integer.parseInt(etTotalE1.getText().toString());
				}
				if (etTotalE2.getText().toString().isEmpty()) {
					tot_p = Integer.parseInt(etTotalE2.getText().toString());
				} else {
					tot_p = Integer.parseInt(etTotalE2.getText().toString());
				}
				if (etTotalE3.getText().toString().isEmpty()) {
					tot_ld = Integer.parseInt(etTotalE3.getText().toString());
				} else {
					tot_ld = Integer.parseInt(etTotalE3.getText().toString());
				}
				if (etTotalE4.getText().toString().isEmpty()) {
					tot_cte = Integer.parseInt(etTotalE4.getText().toString());
				} else {
					tot_cte = Integer.parseInt(etTotalE4.getText().toString());
				}
				if (etTotalE5.getText().toString().isEmpty()) {
					tot_mc = Integer.parseInt(etTotalE5.getText().toString());
				} else {
					tot_mc = Integer.parseInt(etTotalE5.getText().toString());
				}
				if (etTotalE6.getText().toString().isEmpty()) {
					tot_l1 = 0;
				} else {
					tot_l1 = Integer.parseInt(etTotalE6.getText().toString());
				}
				if (etTotalE7.getText().toString().isEmpty()) {
					tot_r = 0;
				} else {
					tot_r = Integer.parseInt(etTotalE7.getText().toString());
				}

				if ((tot_e < obj.getP5_Tot_E() || tot_p < obj.getP5_Tot_P()
						|| tot_ld < obj.getP5_Tot_LD()
						|| tot_cte < obj.getP5_Tot_CTE() || tot_mc < obj
						.getP5_Tot_MC())) {
					Toast.makeText(
							CapituloV_Edificaciones_Activity.this,
							"Debe ingresar valores"
									+ " mayores a los guardados",
							Toast.LENGTH_SHORT).show();
				} else {
					Bundle b = getIntent().getExtras();
					obj.setId_local(id_local);
					obj.setNro_Pred(P5_NroPred);
					obj.setP5_Tot_E(Integer.parseInt(etTotalE1.getText()
							.toString()));
					obj.setP5_Tot_P(Integer.parseInt(etTotalE2.getText()
							.toString()));
					obj.setP5_Tot_LD(Integer.parseInt(etTotalE3.getText()
							.toString()));
					obj.setP5_Tot_CTE(Integer.parseInt(etTotalE4.getText()
							.toString()));
					obj.setP5_Tot_MC(Integer.parseInt(etTotalE5.getText()
							.toString()));
					obj.setP5_Tot_L1(Integer.parseInt(etTotalE6.getText()
							.toString()));
					obj.setP5_Tot_R(Integer.parseInt(etTotalE7.getText()
							.toString()));
					obj.setSwCompleto(true);
					obj.setP5_Obs(etObs.getText().toString());
					cap5Dao.actualizarP5Edificacion(obj);

					Intent irDibujo_Tecnico = new Intent(this,
							CapituloV_Menu.class);
					b.putInt("total_edificaciones",
							Integer.parseInt(etTotalE1.getText().toString()));
					irDibujo_Tecnico.putExtras(b);
					startActivity(irDibujo_Tecnico);
					finish();
				}
			}
			break;
		case R.id.mnFoto:
			Intent foto = new Intent(CapituloV_Edificaciones_Activity.this,
					CapituloIX_Activity.class);
			// b.putBoolean("pregunta", true);
			foto.putExtras(getIntent().getExtras());
			foto.putExtra("pregunta", true);
			startActivity(foto);
			break;

		/*
		 * case R.id.mnCapituloIII: Intent capiii = new Intent(this,
		 * FrmPuntosGPS.class); capiii.putExtras(getIntent().getExtras());
		 * startActivity(capiii); finish(); break; case R.id.mnCapituloIV:
		 * Intent irDibujoIV = new Intent(this,
		 * CapituloV_Dibujo_Tecnico_Activity.class);
		 * irDibujoIV.putExtras(getIntent().getExtras());
		 * startActivity(irDibujoIV); break; case R.id.mnCapituloV: Intent
		 * irCapV = new Intent(this, CapituloV_Dibujo_Tecnico_Activity.class);
		 * irCapV.putExtras(getIntent().getExtras()); startActivity(irCapV);
		 * finish(); break; case R.id.mnCapituloVI: Toast.makeText(this,
		 * "Este es el Capitulo VII", Toast.LENGTH_SHORT) .show(); break; case
		 * R.id.mnCapituloVII: // guardar(); Intent irCapVII = new Intent(this,
		 * CapituloVII_Activity.class);
		 * irCapVII.putExtras(getIntent().getExtras()); startActivity(irCapVII);
		 * finish(); break;
		 */
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Intent irPredio = new Intent(CapituloV_Edificaciones_Activity.this,
				CapituloV_Menu.class);
		Bundle f = getIntent().getExtras();
		f.putString("id_local", id_local);
		f.putInt("Nro_Pred", P5_NroPred);
		irPredio.putExtras(f);
		startActivity(irPredio);
		finish();
		// Toast.makeText(CapituloV_Edificaciones_Activity.this,
		// "Continue con el proceso", Toast.LENGTH_SHORT).show();
	}
}
